package actions;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
import service.ExpressShipPathService;
import domain.ExpressShipPath;
/**
 * @author system auto
 */
public class ExpressShipPathUpdateAction extends Action {
	/**
	 * 用于查询快递线路.
	 */
	private ExpressShipPathService expressShipPathService = null;
	/**
	 * 修改快递线路.
	 * @param mapping action mapping
	 * @param form action form
	 * @param request http request
	 * @param response http response
	 * @return action forword
	 * @throws Exception any exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	       HttpServletRequest request, HttpServletResponse response)
	       throws Exception {
	   	// 验证是否是有效的Token,防止重复提交
	   	if (!isTokenValid(request, true)) {
	    	// 重复提交,转到重复提交失败界面
	    	return mapping.findForward("error.duplicateSubmit");
	   	}
		try{
			DynaValidatorForm expressShipPathForm = (DynaValidatorForm) form;
		 	String id = expressShipPathForm.getString("id");
			ExpressShipPath expressShipPath = expressShipPathService.getExpressShipPathById(id);
			this.convertBean(expressShipPath,expressShipPathForm);
	       	expressShipPathService.updateExpressShipPath(expressShipPath);
	    }catch(Exception e){
	        ActionErrors errors=new ActionErrors();
	        errors.add("errors.data.operate.error", new ActionMessage("errors.data.operate.error",utils.DaoUtil.exceptionToStr(e)));
	        this.addErrors(request, errors);
	           // 设置token 防止重复提交
	        this.saveToken(request);
	        return mapping.getInputForward();
	    }
	    return mapping.findForward("success");
    }
    /**
     *convertBean
     */
     private void convertBean(ExpressShipPath expressShipPath, DynaValidatorForm expressShipPathForm)
	 		throws Exception{
		String startPos=expressShipPathForm.getString("startPos");
		expressShipPath.setStartPos(startPos);
		String endPos=expressShipPathForm.getString("endPos");
		expressShipPath.setEndPos(endPos);
		String serviceType=expressShipPathForm.getString("serviceType");
		expressShipPath.setServiceType(serviceType);
		String transPortType=expressShipPathForm.getString("transPortType");
		expressShipPath.setTransPortType(transPortType);
		String roadDistance=expressShipPathForm.getString("roadDistance");
		if(roadDistance!=null&&!roadDistance.equals("")){
			expressShipPath.setRoadDistance(Double.valueOf(roadDistance));
		}else{
			expressShipPath.setRoadDistance(null);
		}
		String estimateHours=expressShipPathForm.getString("estimateHours");
		if(estimateHours!=null&&!estimateHours.equals("")){
			expressShipPath.setEstimateHours(Double.valueOf(estimateHours));
		}else{
			expressShipPath.setEstimateHours(null);
		}
		String estimateCost=expressShipPathForm.getString("estimateCost");
		if(estimateCost!=null&&!estimateCost.equals("")){
			expressShipPath.setEstimateCost(Double.valueOf(estimateCost));
		}else{
			expressShipPath.setEstimateCost(null);
		}
		String quotedPrice=expressShipPathForm.getString("quotedPrice");
		if(quotedPrice!=null&&!quotedPrice.equals("")){
			expressShipPath.setQuotedPrice(Double.valueOf(quotedPrice));
		}else{
			expressShipPath.setQuotedPrice(null);
		}
		String urgentPrice=expressShipPathForm.getString("urgentPrice");
		if(urgentPrice!=null&&!urgentPrice.equals("")){
			expressShipPath.setUrgentPrice(Double.valueOf(urgentPrice));
		}else{
			expressShipPath.setUrgentPrice(null);
		}
		String noteDesc=expressShipPathForm.getString("noteDesc");
		expressShipPath.setNoteDesc(noteDesc);
     }
   /**
     * @param pExpressShipPathService the expressShipPathService to set
     */
    public void setExpressShipPathService(ExpressShipPathService pExpressShipPathService) {
        expressShipPathService = pExpressShipPathService;
    }
}
